package testimage.zhujj.com.gesturedemo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ObjectAnimator animator;
    private ObjectAnimator animator1;
    private ScaleAnimation scaleAnimation;
    private AnimatorSet set;
    private ChangeRankView ranview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ranview = (ChangeRankView) findViewById(R.id.ranview);
        final RelativeLayout rlContainer = (RelativeLayout) findViewById(R.id.tagLayout);
        final ImageView ivTag = (ImageView) findViewById(R.id.iv_tag);
        final TextView tvTag = (TextView) findViewById(R.id.tv_tag_emoji);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
//                starAnim(rlContainer, ivTag, tvTag);
//                ranview.startAnimation();
                ranview.startDownAnimation();
            }
        });
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
//                stopAnim(rlContainer, ivTag, tvTag);
                ranview.startAnimation();
            }
        });
    }

    private void stopAnim(RelativeLayout rlContainer, ImageView ivTag, TextView tvTag) {
        if (null != set) {
            if (set.isRunning()) {
                set.cancel();
            }
            set = null;
        }
        if (null != animator) {
            animator = null;
        }
        if (null != animator1) {
            animator1 = null;
        }
        if (null != scaleAnimation) {
            scaleAnimation.cancel();
            scaleAnimation = null;
        }
        rlContainer.setTranslationX(0);
        ivTag.setTranslationX(0);
        tvTag.clearAnimation();
    }

    private void starAnim(RelativeLayout rlContainer, ImageView ivTag, TextView tvTag) {
        if (null != set) {
            if (set.isRunning()) {
                set.cancel();
            }
            set = null;
        }
        if (null != animator) {
            animator = null;
        }
        if (null != animator1) {
            animator1 = null;
        }
        if (null != scaleAnimation) {
            scaleAnimation.cancel();
            scaleAnimation = null;
        }
        int measuredWidth = rlContainer.getMeasuredWidth();
        animator = ObjectAnimator.ofFloat(rlContainer, View.TRANSLATION_X, -measuredWidth, 0);
        animator.setDuration(500);
        animator.setInterpolator(new DecelerateInterpolator());
        int measuredWidth1 = ivTag.getMeasuredWidth();
        animator1 = ObjectAnimator.ofFloat(ivTag, View.TRANSLATION_X, -measuredWidth1 - measuredWidth, 0);
        animator1.setDuration(800);
        animator1.setInterpolator(new DecelerateInterpolator());
        scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f);
        scaleAnimation.setStartOffset(500);
        scaleAnimation.setDuration(1500);
        scaleAnimation.setInterpolator(new DesignBounceInterpolator());
        set = new AnimatorSet();
        set.playTogether(animator, animator1);
        set.start();
        tvTag.startAnimation(scaleAnimation);
    }
}
